package cn.pro.utils;

import java.util.List;

public class PageData<T> {

    private long pageIndex;  //当前页码
    private long pageSize;  //页面大小
    private long totalCount; //总条数
    private long pageCount; //总条数


    private List<T> records; //每页的数据集合

    public PageData<T> startPage(List<T> records, Integer pageIndex,
                                 Integer pageSize) {
        this.pageIndex=pageIndex;
        this.pageSize = pageSize;
        if (records == null) {
            return null;
        }
        if (records.size() == 0) {
            return null;
        }

        Integer count = records.size(); // 记录总数
        this.totalCount = count;
        Integer pageCountV = 0; // 页数
        if (count % pageSize == 0) {
            pageCountV = count / pageSize;
        } else {
            pageCountV = count / pageSize + 1;
        }

        int fromIndex = 0; // 开始索引
        int toIndex = 0; // 结束索引

        if (pageIndex != pageCountV) {
            fromIndex = (pageIndex - 1) * pageSize;
            toIndex = fromIndex + pageSize;
        } else {
            fromIndex = (pageIndex - 1) * pageSize;
            toIndex = count;
        }

        List<T> pageList = records.subList(fromIndex, toIndex);
        this.pageCount = pageList.size();
        this.records = pageList;

        return this;
    }


    public long getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(long pageIndex) {
        this.pageIndex = pageIndex;
    }

    public long getPageSize() {
        return pageSize;
    }

    public void setPageSize(long pageSize) {
        this.pageSize = pageSize;
    }

    public long getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(long totalCount) {
        this.totalCount = totalCount;
    }

    public long getPageCount() {
        return pageCount;
    }

    public void setPageCount(long pageCount) {
        this.pageCount = pageCount;
    }


    public List<T> getRecords() {
        return records;
    }

    public void setRecords(List<T> records) {
        this.records = records;
    }
}
